const mgModel = require('../model/managerModel.js')
const verify = require('../config/verify.js')
const url = require('url');
const md5Util = require('../config/md5Util.js')
const dateFormat = require('../config/dateFormat.js')

module.exports.login = (req, res) => {
	let {
		username,
		userpwd
	} = req.body;
	userpwd = md5Util.md5(md5Util.md5(username + md5Util.md5(userpwd)))
	let userInfo = [username, userpwd];
	mgModel.login(userInfo).then(result => {
		if (result.length > 0) {
			let sul = result[0];
			//生成token
			let token = verify.setToken(sul.mg_id, sul.role_id);
			res.send({
				"data": {
					"id": sul.mg_id,
					"rid": sul.role_id,
					"username": sul.mg_name,
					"mobile": sul.mg_mobile,
					"email": sul.mg_email,
					"token": token
				},
				"meta": {
					"msg": "登录成功",
					status: 200
				}
			})
		} else {
			res.send({
				meta: {
					msg: '账户名或密码错误',
					status: 402
				}
			})
		}
	})
}

//返回指定页码的数据
module.exports.getMgByPage = async (req, res) => {
	let {query} = url.parse(req.url, true);
	let {querys,pagenum,pagesize} = query;
	//如果是带条件的查询  页码重置为1
	pagenum = parseInt(pagenum);
	pagesize = parseInt(pagesize);
	//获得表中总共有多少行数据
	//如果是带条件查询 就去获得 过滤之后剩下的条数
	//不带条件查询直接获得总条数
	let count = await mgModel.getMgCount(querys);
	count = count[0].cou;
	//计算总共有多少页
	let totalPage = count % pagesize == 0 ? count / pagesize : count / pagesize + 1;
	totalPage = Math.ceil(totalPage) //4
	//计算起始下标
	let offset = (pagenum - 1) * pagesize;
	if (offset >= count) {
		offset = 0;
		pagenum = 1;
	}
	//定义给查询语句传参的数组
	let queryInfo = [querys, offset, pagesize];
	let managers = await mgModel.getMgByQuerys(queryInfo);
	let users = [];
	for (i in managers) {
		let manager = managers[i];
		let roleName = manager.role_name;
		//如果角色id等于0  角色名称重新命名成超级管理员
		if (!manager.role_id) {
			roleName = '超级管理员';
		}
		users.push({
			"id": manager.mg_id,
			"username": manager.mg_name,
			"mobile": manager.mg_mobile,
			"type": 1,
			"email": manager.mg_email,
			"create_time": manager.mg_time,
			"mg_state": manager.mg_state == 1, // 当前用户的状态
			"role_name": roleName
		})
	}
	res.send({
		data:{
			'totalPage': totalPage,
			'pagenum': pagenum,
			'totalCount': count,
			'users': users
		},
		meta:{
			msg: '用户列表获取成功',
			status: 200
		}
	})
}
//添加用户
module.exports.addUser = async (req,res) =>{
	let {username,userpwd,useremail,usermobile} = req.body;
	console.log(username,userpwd,useremail,usermobile)
	//多层加密  让MD5解析器  不能解析成功
	userpwd = md5Util.md5(md5Util.md5(username + md5Util.md5(userpwd)))
	//定义给sql语句传参的数组
	let userInfo = [username,userpwd,dateFormat.DateFormat(),usermobile,useremail]
	let result = await mgModel.addUser(userInfo)
	if(result.affectedRows >0){
		res.send({
			meta:{
				msg:'用户添加成功',
				status:200
			}
		})
	}else{
		res.send({
			meta:{
				msg:'用户添加失败',
				status:500
			}
		})
	}
}
//改变用户的状态
module.exports.changeState = async (req,res) =>{
	//获取动态路由接收到的数据
	let {uId,stat} = req.params;
	if(stat =='true'){
		stat = 1;
	}else{
		stat = 0;
	}
	console.log(uId,stat)
	//定义给sql语句传参的数组
	let stateInfo = [stat,uId];
	let result = await mgModel.setMgState(stateInfo);
	if(result.affectedRows >0){
		res.send({
			meta:{
				msg: "用户状态修改成功",
				status: 200
			}
		})
	}else{
		res.send({
			meta:{
				msg: "用户状态修改失败",
				status: 500
			}
		})
	}
}
//修改用户信息
module.exports.editUser = async (req,res) =>{
	let {userId,useremail,usermobile} = req.body;
	// console.log(userId,useremail,usermobile)
	//创建给sql语句传参的数组
	let editInfo = [useremail,usermobile,userId];
	let result = await mgModel.editUser(editInfo);
	if(result.affectedRows >0){
		res.send({
			meta:{
				msg: "用户信息修改成功",
				status: 200
			}
		})
	}else{
		res.send({
			meta:{
				msg: "用户信息修改失败",
				status: 500
			}
		})
	}
}
//删除用户信息
module.exports.deleteUser = async (req,res) =>{
	let {uid} = req.params;
	// console.log(uid);
	let result = await mgModel.deleteUser(uid);
	if(result.affectedRows >0){
		res.send({
			meta:{
				msg: "用户信息删除成功",
				status: 200
			}
		})
	}else{
		res.send({
			meta:{
				msg: "用户信息删除失败",
				status: 500
			}
		})
	}
}

module.exports.setMgRole = async (req,res) =>{
	// 获取动态路由接收到的数据
	let {rid,uid} = req.params;
	// console.log(rid,uid);
	//创建给sql语句传参的数组
	let mgInfo = [rid,uid];
	let result = await mgModel.setMgRole(mgInfo);
	if(result.affectedRows >0){
		res.send({
			meta:{
				msg: "角色修改成功",
				status: 200
			}
		})
	}else{
		res.send({
			meta:{
				msg: "角色修改失败",
				status: 500
			}
		})
	}
}